﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BT_BieuThuc_Nhom13.Converter;

namespace BT_BieuThuc_Nhom13.Expressions
{
    public class BinaryExpr : Expression
    {
        protected Expression expr1;
        protected Expression expr2;

        protected MyConverter Converter;

        public override void SetConverter(MyConverter newConverter)
        {
            Converter = newConverter;
            expr1.SetConverter(Converter);
            expr2.SetConverter(Converter);
        }

        public override string ToString()
        {
            String Result = String.Empty;
            if (Converter == null)
            {
                return "Chua set Converter";
            }

            List<OrderIndex> lstOrderFormated = Converter.GetListFormat();
            if (lstOrderFormated == null)
            {
                return Result;
            }


            foreach (OrderIndex order in lstOrderFormated)
            {
                Result += GetStringFromOrder(order);
            }

            return Result;
        }

        protected virtual String Operator()
        {
            return String.Empty;
        }

        protected String GetStringFromOrder(OrderIndex order)
        {
            switch (order)
            {
                case OrderIndex.FirstNumber:
                    return expr1.ToString();

                case OrderIndex.SecondNumber:
                    return expr2.ToString();

                case OrderIndex.Operator:
                    return Operator();
            }

            return String.Empty;
        }
    }
}
